<?php
namespace app\subscription\controller;

use app\manage\model\AdminUser;
use app\manage\model\ApartmentSettings;
use app\manage\model\ContracTmanagement;
use app\manage\model\HousingInformation;
use app\manage\model\Shops;
use app\manage\model\ElectricityBill;
use app\manage\model\EstateBill;
use app\manage\model\GasBill;
use app\manage\model\NetBill;
use app\manage\model\WaterBill;
use think\Controller;
use think\Request;
use think\Validate;

class PushMessage extends Controller {
    private $appid;
    private $pagepath;
    private $template_id;

    public function __construct(Request $request = null)
    {
        $this->appid = config('applet')['tenant']['appid'];
        $this->pagepath = 'pages/mine/payment/eletric/eletricDetail/eletricDetail';
        $this->template_id = 'mTXZegH-NDoXLYHgmXfENjYheLnlRSb1Vdz2HC9mgV4';
    }

    /**
     * 账单消息推送
     */
    private function BillInformation($tel, $house_id, $type) {

        $model_e = new ElectricityBill();
        $model_w = new WaterBill();
        $model_g = new GasBill();
        $model_n = new NetBill();
        $model_s = new EstateBill();

        $list['e'] = $model_e->getUnpay($house_id, $type);
        $list['w'] = $model_w->getUnpay($house_id, $type);
        $list['g'] = $model_g->getUnpay($house_id, $type);
        $list['n'] = $model_n->getUnpay($house_id, $type);
        $list['s'] = $model_s->getUnpay($house_id, $type);

        $WeChat = new  \Template\Wechat();
        $weiwei_token = $WeChat->getToken();// 获取微信token
        $openid = $WeChat->getUserInfo($tel);

        if ($openid !== false) {
            foreach ($list as $k => $value) {
                if (empty($value)) continue;
                # 公众号消息推送
                $i = $WeChat::pushMessage([
                    'miniprogram' => [
                        'appid' => $this->appid,
                        'pagepath' => $this->pagepath . '?id=' . $value['id'] . '&type=' . $value['type'],
                    ],
                    'openid' => $openid, // 用户openid
                    'access_token' => $weiwei_token,
                    'template_id' => $this->template_id, // 填写你自己的消息模板ID
                    'data' => [ // 模板消息内容，根据模板详情进行设置
                        'first' => ['value' => urlencode("缴费通知。账单已生成，请尽快缴费"), 'color' => "#743A3A"],
                        'keyword1' => ['value' => urlencode($value['tung_num'] . '-' . $value['unit_num'] . '-' . $value['room_number'])],//房号
                        'keyword2' => ['value' => urlencode($value['tenant_name'])],//租客
                        'keyword3' => ['value' => urlencode($this->getBillType($k))],//账单类型
                        'keyword4' => ['value' => urlencode("未缴费")],//未缴费
                        'keyword5' => ['value' => urlencode('¥' . $value['cost'])],//合计金额
                        'remark' => ['value' => urlencode("请您及时缴费。"), 'color' => '#743A3A']
                    ],
                ]);
                $data['openid'] = $openid;
                $data['icode'] = $i;
            }
        }
//        returnJson(200,$data)->send();
    }

    private function getBillType($type) {
        switch ($type) {
            case 'e':
                return '电费';
            case 'w':
                return '水费';
            case 'g':
                return '气费';
            case 'n':
                return '网费';
            case 's':
                return '物业费';
            default:
                return '未知类型';
        }
    }

    /**
     * 推送消息
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function pushInformation() {
        $model = new AdminUser();
        $model_h = new HousingInformation();
        $model_s = new Shops();
        $contract = new ContracTmanagement();
        $plat_list = $model->m_list(1);

        foreach ($plat_list as $plat) {
            $model = new ApartmentSettings();
            $set = $model->index2($plat['admin_id']);
            if ($set == null) continue;
            if ($set['settlement_time'] != date('d', time())) continue;
            $list_h = $model_h->indexLease($plat['admin_id']);
            foreach ($list_h as $house) {
                $detail = $contract->HouseTenantInfo($house['housing_id'], 1);
                if ($detail['tenant_tel'] == null) continue;
                $this->BillInformation($detail['tenant_tel'], $house['housing_id'], 1);
            }
            $list_s = $model_s->indexLease($plat['admin_id']);
            foreach ($list_s as $shop) {
                $detail = $contract->HouseTenantInfo($shop['shops_id'], 2);
                if ($detail['tenant_tel'] == null) continue;
                $this->BillInformation($detail['tenant_tel'], $shop['shops_id'], 2);
            }
        }

    }
}